package com.atxiaodei.kabudasockerserverdemo;

import java.io.*;
import java.net.Socket;

/**
 * @ClassName SocketThread
 * @Deacription TODO
 * @Author 卡布达
 * @Date 2021/10/27 16:11
 * @Version 1.0
 **/
public class SocketThread implements Runnable{
    private Socket socket;

    public SocketThread(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        try {
            // 由Socket对象得到输入流，并构造相应的 BufferedReader
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String clientStr=bufferedReader.readLine();// 读取客户端的一行数据

            System.out.println("接收到客户端的信息"+clientStr);

            // 服务端接收到消息向客户端应答
            // 由Socket对象得到输入流，并构造相应的 BufferedWriter
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bufferedWriter.write("服务端我收到了你客户端的消息了\n");

            bufferedWriter.flush();
            bufferedWriter.close();
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
